<?php
class FormcategoryModel extends Model {
    public $list;
	//获取分类列表，并带上‘深度’
	public function getList($pid=0,$i=0,$id=0){
        $map['pid']=$pid;
		$map['site_id']=__SITEID__;
        if($id!=0){
            $map['id']=$id;    
        }
		$re = $this->where($map)->select();
		if(false == $re) return null;
		foreach ($re as $v){
			$v['cl']=$i;
		    $this->list[]=$v;
			array_merge($this->list,$this->getList($v['id'],$i+1));
		}
		return $this->list;
	}
	
	//输入一个ID，递归出一个上级ID为0的ID
	public function getTopid($pid){
		if($pid){
			$ret=$this->where(['id'=>$pid])->find();
			if($ret['pid']==0){
				return $pid;
			}else{
				return $this->getTopid($ret['pid']);
			}
		}else{
			return $pid;	
		}
	}
	
	//复制表格分类
	public function copy($siteid, $tositeid,$map, $pid = 0){
		$re=$this->where($map)->order('sort,id')->select();
		$FormDB = D('Form');
		if($re!==false){
			foreach($re as $k=>$v){ 
				$v['relate_id'] = $v['id'];
				$map['pid'] = $v['id'];
				$v['site_id'] = $tositeid;
				
				unset($v['id']);
				$v['pid'] = $pid;
				$pid1 = $this->data($v)->add();
				
				echo '<script>window.parent.progress("写入一个表格分类，ID为：'.$pid1.'");</script>';
				ob_flush();
                flush();
				//导入产品和新闻
				$FormDB -> copy($siteid, $tositeid, array('formcategory_id' => $v['relate_id'],'site_id'=>$siteid), $pid1);
				$this->copy($siteid, $tositeid,$map, $pid1);
			}
			return true;
		}
	}
	
	
}